using System;
using System.Data;
using System.Data.OleDb;
using MySql.Data.MySqlClient;
using System.IO;
using System.Web;
namespace LdExpress
{
    /// <summary>
    /// 
    /// </summary>
    public class Location
    {
        public long ID = 0;
        public string Code = "";
        public string Name = "";
        public string Note = "";
        public Location()
        {
        }
        public Location(long lID)
        {
            this.ID = lID;
        }
        public void Save(MySqlConnection cn, MySqlTransaction trans)
        {
            string sqlCommand = "";
            if (ID <= 0) // new object, we insert new record to database
            {
                // build SQL statement
                sqlCommand = "Insert into tbl_Location (" +
                             "Code," +
                             "Name," +
                             "Note) " +
                         " values(" +
                             "'" + DBModule.RefineString(Code) + "'," +
                             "'" + DBModule.RefineString(Name) + "'," +
                             "'" + DBModule.RefineString(Note) + "')";
                // run SQL statement
                DBModule.ExecuteNonQuery(sqlCommand, cn, trans);
                UserObjectsLog.SaveObjectActionLog("Location", "add", cn, trans);              
            }
            else // edit object, we update old record in database
            {
                // build SQL statement
                sqlCommand = "Update tbl_Location set " +
                             "ID=" + ID.ToString() + "," +
                             "Code='" + DBModule.RefineString(Code) + "'," +
                             "Name='" + DBModule.RefineString(Name) + "'," +
                             "Note='" + DBModule.RefineString(Note) + "'" +
                    " where ID=" + ID.ToString();
                // run SQL statement
                DBModule.ExecuteNonQuery(sqlCommand, cn, trans);
                UserObjectsLog.SaveObjectActionLog("Location", "edit", cn, trans);                
            }
        }
        public void Delete(MySqlConnection cn, MySqlTransaction trans)
        {
            //Buil SQL Statement
            //Run SQL Statement
            string sqlCommand = "DELETE FROM tbl_Location WHERE ID = " + this.ID.ToString() + " limit 1";
            DBModule.ExecuteNonQuery(sqlCommand, cn, trans);
            UserObjectsLog.SaveObjectActionLog("Location", "delete", cn, trans);
        }
        public void Load(MySqlConnection cn, MySqlTransaction trans)
        {
            string sqlCommand = "";
            //Build SQL Statement
            //Run SQL Statement
            sqlCommand = "SELECT * FROM tbl_Location WHERE ID = " + ID.ToString() + " limit 0,1";
            DataSet ds = DBModule.ExecuteQuery(sqlCommand, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID"))
                    ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("Code"))
                    Code = dr["Code"].ToString();
                if (!dr.IsNull("Name"))
                    Name = dr["Name"].ToString();
                if (!dr.IsNull("Note"))
                    Note = dr["Note"].ToString();
            }
            else
            {
            }
        }
        public static void GetList(string Condition, string OrderBy, int NumOfItemsPerPage, int PageNo, out int NumberOfPages, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            NumberOfPages = 0;
            string sqlCommand = "";
            ds = null;
            // build SQL statement
            sqlCommand = "Select * from tbl_Location where 1=1 ";
            if (Condition.Length > 0)
            {
                sqlCommand += Condition;
            }

            if ((OrderBy != null) && (OrderBy != ""))
                sqlCommand = sqlCommand + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            if (NumOfItemsPerPage > 0)
            {
                ds = DBModule.ExecuteQuery(sqlCommand, NumOfItemsPerPage, PageNo, out NumberOfPages, cn, trans);
            }
            else
                ds = DBModule.ExecuteQuery(sqlCommand, cn, trans);
        }
        public static void DoSearch(string Codition, string OrderBy, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            string sqlCommand = "";
            ds = null;
            // build SQL statement
            sqlCommand = "Select * from tbl_Location where 1=1 ";
            if (Codition.Length > 0)
                sqlCommand += Codition;
            if ((OrderBy != null) && (OrderBy != ""))
                sqlCommand = sqlCommand + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            ds = DBModule.ExecuteQuery(sqlCommand, cn, trans);
        }
    }
}
